const cheerio = require('cheerio');
const charset = require('superagent-charset');
const superagent = charset(require('superagent'));
const fs = require('fs');
const query = require('./db');
const util = require('./util');
const baseUrl = 'https://www.biquge5200.com/';

let fetchChapters = [];
let currChapter = 0;

getChapters('3_3372')
    .then((data) => {
        for (let value of data.chapters) {
            fetchChapters.push(value.chapter_href);
        }
        // console.log(fetchChapters);
        let sql = "select book_id,last_chapter,last_chapter_url from book where book_url = ?";
        query(sql, [data.url])
            .then((res) => {
                res = res[0];
                let last_chapter_url = res.last_chapter_url;
                let book_id = res.book_id;
                if (!res) {
                    let booksql = 'insert into book(book_title,book_author,book_url,last_chapter,class_id) values(?,?,?,?,?)';
                    let bookOptions = [data.title, data.author, data.url, data.last_chapter, '1'];
                    query(booksql, bookOptions)
                        .then((res) => {
                            console.log('insert book');
                            console.log('get content start')
                            // getNovelContent(fetchChapters[currChapter]);
                        })
                } else {
                    if (res.last_chapter != data.last_chapter) {
                        // 获取当前爬取页面的最新章节
                        let lst_ch = data.last_chapter;
                        lst_ch = lst_ch.replace(baseUrl, '');
                        let sql = 'update book set last_chapter=?,last_chapter_url=? where book_id=?';
                        console.log('update chapter');
                        let len = fetchChapters.length;
                        let new_last_chapter_url = baseUrl + last_chapter_url;
                        let index = fetchChapters.findIndex((item) => {
                            return item == new_last_chapter_url;
                        });
                        let newFetchChapters = fetchChapters.slice(index + 1, len);
                        console.log(newFetchChapters);
                        // 更新
                        query(sql, [data.last_chapter, lst_ch, book_id])
                            .then((res) => {

                            })
                    }else{
                        console.log('没有更新~~');
                        process.exit(-1);
                    }
                }
            })


    })

function getChapters(novel_id) {
    return new Promise((resolve, reject) => {
        let data = {};
        let chapters = [];
        let last_chapter = '';
        superagent
            .get(`${baseUrl}${novel_id}`)
            .charset('gbk')
            .end((err, res) => {
                if (err) {
                    reject(err)
                } else {
                    let $ = cheerio.load(res.text);
                    $("#list dl dd a").each((i, elem) => {
                        let _this = $(elem);
                        chapters.push({
                            'chapter_title': _this.text(),
                            'chapter_href': _this.attr('href')
                        })
                    })
                    last_chapter = chapters[0].chapter_title;
                    for (let i = 0; i < 9; i++) {
                        chapters.splice(0, 1);
                    }
                    let title = $("#info h1").text();
                    let author = $("#info p").eq(0).text();
                    author = author.substr(author.indexOf('：') + 1, author.length);
                    data = {
                        title: title,
                        author: author,
                        url: novel_id,
                        last_chapter: last_chapter,
                        chapters: chapters,
                    };
                    resolve(data)
                }
            })
    })

}

// getNovelContent('https://www.biquge5200.com/76_76934/147230933.html');
function getNovelContent(url) {
    console.log(`get url ${url} currChapter ${currChapter}`);
    superagent
        .get(url)
        .charset('gbk')
        .end((err, res) => {
            let $ = cheerio.load(res.text);
            let content = $("#content").html();
            let title = $(".bookname h1").text();
            content = util.unicodeToCh(content);
            fs.writeFile(`../novel_data/${title}.txt`, content, (err) => {
                if (err) {
                    console.log({ err });
                } else {
                    currChapter++;
                    if (currChapter < fetchChapters.length) {

                        setTimeout(() => {
                            getNovelContent(fetchChapters[currChapter]);
                        }, 100)
                    } else {
                        console.log('end');
                        process.exit(1);
                    }

                }
            })
        })
}

// unicode 转中文
// function unicodeToCh(str) {
//     let newStr = unescape(str.replace(/\\u/g, "%u"));
//     newStr = newStr.replace(/&#(x)?(\w+);/g, function ($, $1, $2) {
//         return String.fromCharCode(parseInt($2, $1 ? 16 : 10));
//     });
//     return newStr;
// }

